/**
 * Copyright (c) Software Institute of PKU
 * All rights reserved. 
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 */
package edu.pku.sei.modeler.edit.parts;

import org.eclipse.draw2d.ConnectionEndpointLocator;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.PolylineDecoration;
import org.eclipse.draw2d.RotatableDecoration;

import edu.pku.sei.modeler.model.structure.MetaShapeElement;

/**
 * @author zl
 *
 */
public class DefaultLinkEditPart extends AbstractLinkEditPart {
	protected Label srcCount, srcName, targetCount, targetName, assoName;
	protected ConnectionEndpointLocator srcCountLocator, srcNameLocator, 
			targetCountLocator, targetNameLocator, assoNameLocator;
	protected ConnectionMidPointLocator midDecorationLoacator;
	protected RotatableDecoration srcDecor, targetDecor;
	
	public DefaultLinkEditPart(MetaShapeElement conn) {
		super(conn);
		// TODO Auto-generated constructor stub
	}
	protected IFigure createFigure() {
		PolylineConnection conn = new PolylineConnection();

		// Create the endpoint locators
		srcCountLocator = new ConnectionEndpointLocator(conn, false);
		srcNameLocator = new ConnectionEndpointLocator(conn, false);
		srcNameLocator.setVDistance(-4);
		targetCountLocator = new ConnectionEndpointLocator(conn, true);
		targetNameLocator = new ConnectionEndpointLocator(conn, true);
		targetCountLocator.setVDistance(-4);
		
		//Create the midpoint locators
		midDecorationLoacator = new ConnectionMidPointLocator(conn);
		//end add
		return conn;
	}

	protected void refreshVisuals() {
		super.refreshVisuals();
		updateSourceDecoration();
		updateTargetDecoration();
		updateSourceCount();
		updateSourceName();
		updateTargetName();
		updateName();
	}
	
	private PolylineConnection getConnection() {
		return (PolylineConnection)getFigure();
	}
	
	private PolylineDecoration createPolylineDecoration () {
		PolylineDecoration decoration = new PolylineDecoration();
		decoration.setScale(10, 5);
		return decoration;
	}
	
	private void updateName(){
		if(assoName == null){
			assoName = new Label();
			assoName.setOpaque(true);
			getConnection().add(assoName,midDecorationLoacator);
		}
		assoName.setText("Default Link");
	}
	//end add
	
	private void updateSourceDecoration() {
		
	}
	
	private void updateTargetDecoration() {
		if (targetDecor == null) {
			targetDecor = createPolylineDecoration();
			getConnection().setTargetDecoration(targetDecor);
		}
	}
	
	private void updateSourceCount() {
		
	}
	
	private void updateSourceName() {
		if (srcName == null) {
			srcName = new Label();
			srcName.setOpaque(true);
			getConnection().add(srcName, srcNameLocator);
		}
//		Property srcProp = (Property)getAssociation().getMemberEnd().get(0); 
//		srcName.setText(srcProp.getName());
	}
	
	private void updateTargetName() {
		if (targetName == null) {
			targetName = new Label();
			targetName.setOpaque(true);
			getConnection().add(targetName, targetNameLocator);
		}
//		targetName.setText(getAssociation().getName());
	}
}
